home *** CD-ROM | disk | FTP | other *** search
- # Source Generated with Decompyle++
- # File: in.pyc (Python 2.4)
-
- '''IPv6 helper functions.'''
- import re
- import dns.exception as dns
- import dns.ipv4 as dns
- _leading_zero = re.compile('0+([0-9a-f]+)')
-
- def inet_ntoa(address):
- """Convert a network format IPv6 address into text.
-
- @param address: the binary address
- @type address: string
- @rtype: string
- @raises ValueError: the address isn't 16 bytes long
- """
- if len(address) != 16:
- raise ValueError, 'IPv6 addresses are 16 bytes long'
-
- hex = address.encode('hex_codec')
- chunks = []
- i = 0
- l = len(hex)
- while i < l:
- chunk = hex[i:i + 4]
- m = _leading_zero.match(chunk)
- if m is not None:
- chunk = m.group(1)
-
- chunks.append(chunk)
- i += 4
- best_start = 0
- best_len = 0
- start = -1
- last_was_zero = False
- for i in xrange(8):
- if chunks[i] != '0':
- if last_was_zero:
- end = i
- current_len = end - start
- if current_len > best_len:
- best_start = start
- best_len = current_len
-
- last_was_zero = False
-
- last_was_zero
- if not last_was_zero:
- start = i
- last_was_zero = True
- continue
-
- if last_was_zero:
- end = 8
- current_len = end - start
- if current_len > best_len:
- best_start = start
- best_len = current_len
-
-
- if best_len > 0:
- if best_start == 0:
- pass
- None if (best_len == 6 or best_len == 5) and chunks[5] == 'ffff' else chunks[5] == 'ffff'
- hex = ':'.join(chunks[:best_start]) + '::' + ':'.join(chunks[best_start + best_len:])
- else:
- hex = ':'.join(chunks)
- return hex
-
- _v4_ending = re.compile('(.*):(\\d+)\\.(\\d+)\\.(\\d+)\\.(\\d+)$')
- _colon_colon_start = re.compile('::.*')
- _colon_colon_end = re.compile('.*::$')
-
- def inet_aton(text):
- '''Convert a text format IPv6 address into network format.
-
- @param text: the textual address
- @type text: string
- @rtype: string
- @raises dns.exception.SyntaxError: the text was not properly formatted
- '''
- if text == '::':
- text = '0::'
-
- m = _v4_ending.match(text)
- if m is not None:
- text = '%s:%04x:%04x' % (m.group(1), int(m.group(2)) * 256 + int(m.group(3)), int(m.group(4)) * 256 + int(m.group(5)))
-
- m = _colon_colon_start.match(text)
- if m is not None:
- text = text[1:]
- else:
- m = _colon_colon_end.match(text)
- if m is not None:
- text = text[:-1]
-
- chunks = text.split(':')
- l = len(chunks)
- if l > 8:
- raise dns.exception.SyntaxError
-
- seen_empty = False
- canonical = []
- for c in chunks:
- if c == '':
- if seen_empty:
- raise dns.exception.SyntaxError
-
- seen_empty = True
- for i in xrange(0, (8 - l) + 1):
- canonical.append('0000')
-
- lc = len(c)
- if lc > 4:
- raise dns.exception.SyntaxError
-
- if lc != 4:
- c = '0' * (4 - lc) + c
-
- canonical.append(c)
-
- if l < 8 and not seen_empty:
- raise dns.exception.SyntaxError
-
- text = ''.join(canonical)
- return text.decode('hex_codec')
-
-